/* Copyright (C) 
* 2014 - ZengYajian
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
* 
*/

typedef enum {
	PARSE_STATE_IDLE,
	PARSE_STATE_LEAD_1,			/* 9ms leading pulse burst 	*/
	PARSE_STATE_LEAD_0,			/* 4.5ms space 				*/
	PARSE_STATE_LOGIC_0_PULSE,	/* 562.5µs pulse burst		*/
	PARSE_STATE_LOGIC_0_SPACE,	/* 562.5µs space			*/
	PARSE_STATE_LOGIC_1_PULSE,	/* 562.5µs pulse burst		*/
	PARSE_STATE_LOGIC_1_SPACE,	/* 1.6875ms space			*/
	PARSE_STATE_COMPLETE
} parse_state_t;

struct parse_info_t {
	parse_info_t state;

	unsigned int code;
	unsigned int bit_mask;
	unsigned int cycle_cnt;
};

static struct parse_info_t parse_info;

#define CYCLE_BETWEEN(cy, ref, magin)	(((cy) <= (ref) + (magin))&&((cy) >= (ref) - (magin)))

void parse_state_machine(char reg)
{
	unsigned int sign = (reg&0x80)?1:0;
	unsigned int cycle = reg&0x7f;

	switch(parse_info.state) {
		case PARSE_STATE_IDLE:
			if (sign) {
				parse_info.state = PARSE_STATE_LEAD_1;
				parse_info.code = 0;
				parse_info.bit_mask = 0;
				parse_info.cycle_cnt += cycle;
			}
			break;
		case PARSE_STATE_LEAD_1:
			if (sign) {
				parse_info.cycle_cnt += cycle;
			} else {
				if (CYCLE_BETWEEN(parse_info.cycle, 84, 8))
			}
		case PARSE_STATE_LEAD_0:
		case PARSE_STATE_LOGIC_0_PULSE:
		case PARSE_STATE_LOGIC_0_SPACE:
		case PARSE_STATE_LOGIC_1_PULSE:
		case PARSE_STATE_LOGIC_1_SPACE:
		case PARSE_STATE_COMPLETE:
